﻿#region 命名空间

using System;
using System.IO;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.BLL.PageFactory.TagModule;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.DataBase
{
    [Access("271001,271002")]
    public partial class SqlServer : WebFactory.PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var strAction = Request.QueryString["Action"];
                if (strAction == "Backup")
                {
                    this.Panel1.Visible = true;
                    this.Panel2.Visible = false;
                    this.Panel3.Visible = false;
                    this.lblPath.Text = "数据库管理 → SqlServer → 备份数据库";
                    Title = "备份数据库";
                }
                else if (strAction == "Restore")
                {
                    this.Panel1.Visible = false;
                    this.Panel2.Visible = true;
                    this.Panel3.Visible = false;
                    this.lblPath.Text = "数据库管理 → SqlServer → 还原数据库";
                    Title = "还原数据库";
                }
                else if (strAction == "Compact")
                {
                    this.Panel1.Visible = false;
                    this.Panel2.Visible = false;
                    this.Panel3.Visible = true;
                    this.lblPath.Text = "数据库管理 → SqlServer → 压缩数据库";
                    Title = "压缩数据库";
                }
            }
        }

        protected void btnBackup_Click(object sender, EventArgs e)
        {
            var strDataBaseName = TagEngine.GetValue(WebConfig.ConnectionString, "Database");

            var Dir = Path.GetDirectoryName(this.txtBackup.Text.Trim());
            if (Directory.Exists(Dir))
            {
                Directory.CreateDirectory(Dir);
            }

            var strSql = string.Format("backup database {0} to disk='{1}'", strDataBaseName, this.txtBackup.Text.Trim());

            try
            {
                SQLBaseV2.ExecuteNonQuery(strSql);

                MsgBox.Message("备份完成", this);
            }
            catch (Exception error)
            {
                MsgBox.Message(error.Message, this);
            }
        }

        protected void btnRestore_Click(object sender, EventArgs e)
        {
            var strDataBaseName = TagEngine.GetValue(WebConfig.ConnectionString, "Database");

            if (File.Exists(this.txtRestore.Text.Trim()) == false)
            {
                MsgBox.Message("文件不存在！", this);
                return;
            }

            var SqlStr2 = string.Format("use master restore database {0} from disk='{1}'", strDataBaseName,
                this.txtRestore.Text.Trim());

            try
            {
                SQLBaseV2.ExecuteNonQuery(SqlStr2);

                MsgBox.Message("备份完成", this);
            }
            catch (Exception error)
            {
                MsgBox.Message(error.Message, this);
            }
        }

        protected void btnCompact_Click(object sender, EventArgs e)
        {
            var strDataBaseName = TagEngine.GetValue(WebConfig.ConnectionString, "Database");

            var SqlStr2 = string.Format("DBCC SHRINKDATABASE(N'{0}' )", strDataBaseName);

            try
            {
                SQLBaseV2.ExecuteNonQuery(SqlStr2);

                MsgBox.Message("压缩完成", this);
            }
            catch (Exception error)
            {
                MsgBox.Message(error.Message, this);
            }
        }
    }
}